package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.king.image.imageviewer.ImageViewer;
import com.king.image.imageviewer.loader.GlideImageLoader;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.CreateRecordAdapter;
import flc.ast.adapter.UseRecordAdapter;
import flc.ast.bean.MyRecordBean;
import flc.ast.databinding.ActivityRecordBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

public class RecordActivity extends BaseAc<ActivityRecordBinding> {

    private List<StkResBean> listUse = new ArrayList<>();
    private List<MyRecordBean> listCreate = new ArrayList<>();
    private List<StkResBean> listUseDelete = new ArrayList<>();
    private List<MyRecordBean> listCreateDelete = new ArrayList<>();
    private UseRecordAdapter useRecordAdapter;
    private CreateRecordAdapter createRecordAdapter;
    private boolean isUse = true;
    private boolean isDelete = false;

    @Override
    protected int onCreate() {
        return R.layout.activity_record;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivRecordBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.rlUseRecord.setOnClickListener(this);
        mDataBinding.rlCreateRecord.setOnClickListener(this);
        mDataBinding.ivRecordManage.setOnClickListener(this);
        mDataBinding.ivRecordDelete.setOnClickListener(this);
        mDataBinding.rvUseRecordList.setLayoutManager(new GridLayoutManager(mContext, 3));
        useRecordAdapter = new UseRecordAdapter();
        mDataBinding.rvUseRecordList.setAdapter(useRecordAdapter);
        useRecordAdapter.setOnItemClickListener(this);
        mDataBinding.rvCreateRecordList.setLayoutManager(new GridLayoutManager(mContext, 2));
        createRecordAdapter = new CreateRecordAdapter();
        mDataBinding.rvCreateRecordList.setAdapter(createRecordAdapter);
        createRecordAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.rlUseRecord:
                clearSelection();
                mDataBinding.rvUseRecordList.setVisibility(View.VISIBLE);
                mDataBinding.rvCreateRecordList.setVisibility(View.GONE);
                mDataBinding.ivUseRecord.setVisibility(View.VISIBLE);
                mDataBinding.tvUseRecord.setVisibility(View.GONE);
                getUseData();
                isUse=true;
                break;
            case R.id.rlCreateRecord:
                clearSelection();
                mDataBinding.rvUseRecordList.setVisibility(View.GONE);
                mDataBinding.rvCreateRecordList.setVisibility(View.VISIBLE);
                mDataBinding.ivCreateRecord.setVisibility(View.VISIBLE);
                mDataBinding.tvCreateRecord.setVisibility(View.GONE);
                getCreateData();
                isUse=false;
                break;
            case R.id.ivRecordManage:
                isDelete=true;
                useRecordAdapter.setEdit(isDelete);
                useRecordAdapter.notifyDataSetChanged();
                createRecordAdapter.setEdit(isDelete);
                createRecordAdapter.notifyDataSetChanged();
                mDataBinding.ivRecordManage.setVisibility(View.GONE);
                mDataBinding.ivRecordDelete.setVisibility(View.VISIBLE);
                break;
            case R.id.ivRecordDelete:
                isDelete=false;
                useRecordAdapter.setEdit(isDelete);
                useRecordAdapter.notifyDataSetChanged();
                createRecordAdapter.setEdit(isDelete);
                createRecordAdapter.notifyDataSetChanged();
                mDataBinding.ivRecordManage.setVisibility(View.VISIBLE);
                mDataBinding.ivRecordDelete.setVisibility(View.GONE);
                listUse.removeAll(listUseDelete);
                AppSpUtils.saveUseRecordList(listUse);
                listCreate.removeAll(listCreateDelete);
                AppSpUtils.saveCreateRecordList(listCreate);
                if (isUse){
                    getUseData();
                }else {
                    getCreateData();
                }
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == useRecordAdapter) {
            if (isDelete) {
                if (useRecordAdapter.getItem(position).isSelected()) {
                    useRecordAdapter.getItem(position).setSelected(false);
                    useRecordAdapter.notifyItemChanged(position);
                    listUseDelete.remove(useRecordAdapter.getItem(position));
                } else {
                    listUseDelete.add(useRecordAdapter.getItem(position));
                    useRecordAdapter.getItem(position).setSelected(true);
                    useRecordAdapter.notifyItemChanged(position);
                }
            } else {
                ImageViewer.load(useRecordAdapter.getItem(position).getUrl())//要加载的图片数据，单张或多张
                        .selection(0)//当前选中位置，默认：0
                        .indicator(false)//是否显示指示器，默认不显示
                        .imageLoader(new GlideImageLoader())//加载器，imageLoader必须配置，目前内置的有GlideImageLoader或PicassoImageLoader，也可以自己实现
                        .theme(R.style.ImageViewerTheme)//设置主题风格，默认：R.style.ImageViewerTheme
                        .orientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)//设置屏幕方向,默认：ActivityInfo.SCREEN_ORIENTATION_BEHIND
                        .start(RecordActivity.this);//activity or fragment, 跳转时的共享元素视图
            }
        } else if (adapter == createRecordAdapter) {
            if (isDelete) {
                if (createRecordAdapter.getItem(position).isSelected()) {
                    createRecordAdapter.getItem(position).setSelected(false);
                    createRecordAdapter.notifyItemChanged(position);
                    listCreateDelete.remove(createRecordAdapter.getItem(position));
                } else {
                    listCreateDelete.add(createRecordAdapter.getItem(position));
                    createRecordAdapter.getItem(position).setSelected(true);
                    createRecordAdapter.notifyItemChanged(position);
                }
            } else {
                ImageViewer.load(createRecordAdapter.getItem(position).getPath())//要加载的图片数据，单张或多张
                        .selection(0)//当前选中位置，默认：0
                        .indicator(false)//是否显示指示器，默认不显示
                        .imageLoader(new GlideImageLoader())//加载器，imageLoader必须配置，目前内置的有GlideImageLoader或PicassoImageLoader，也可以自己实现
                        .theme(R.style.ImageViewerTheme)//设置主题风格，默认：R.style.ImageViewerTheme
                        .orientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)//设置屏幕方向,默认：ActivityInfo.SCREEN_ORIENTATION_BEHIND
                        .start(RecordActivity.this);//activity or fragment, 跳转时的共享元素视图
            }
        }
    }

    @Override
    protected void initData() {
        getUseData();
    }

    private void getUseData() {
        listUse.clear();
        listUseDelete.clear();
        List<StkResBean> listSp = AppSpUtils.getUseRecordList();
        if (listSp != null && listSp.size() > 0) {
            listUse.addAll(listSp);
            useRecordAdapter.setList(listUse);
            mDataBinding.rvUseRecordList.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rvUseRecordList.setVisibility(View.GONE);
        }
    }

    private void getCreateData() {
        listCreate.clear();
        listCreateDelete.clear();
        List<MyRecordBean> listSp = AppSpUtils.getCreateRecordList();
        if (listSp != null && listSp.size() > 0) {
            listCreate.addAll(listSp);
            createRecordAdapter.setList(listCreate);
            mDataBinding.rvCreateRecordList.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rvCreateRecordList.setVisibility(View.GONE);
        }
    }

    private void clearSelection() {
        mDataBinding.ivUseRecord.setVisibility(View.INVISIBLE);
        mDataBinding.tvUseRecord.setVisibility(View.VISIBLE);
        mDataBinding.ivCreateRecord.setVisibility(View.INVISIBLE);
        mDataBinding.tvCreateRecord.setVisibility(View.VISIBLE);
    }

}